package com.dgut.hisystem.service.impl;

import com.dgut.hisystem.entity.*;
import com.dgut.hisystem.entity.VO.AdviceVO;
import com.dgut.hisystem.entity.VO.CaseVO;
import com.dgut.hisystem.mapper.*;
import com.dgut.hisystem.service.CaseService;
import com.dgut.hisystem.service.RegisterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.PercentEscaper;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CaseServiceImpl implements CaseService {
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private AdviceMapper adviceMapper;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private ChargeMapper chargeMapper;

    @Override
    public List<CaseVO> selectPending(){
        List<Register> registers = registerMapper.getRegister();
        return getCaseVOS(registers);
    }

    @Override
    public List<CaseVO> selectPendingByCard(long br_card){
        List<Register> registers = registerMapper.getRegisterByCard(br_card);
        return getCaseVOS(registers);
    }

    @Override
    public void insertPrescription(List<AdviceVO> adviceVOS,long id){
        double allPrice = 0;
        Case case1 = caseMapper.selectID(id);
        Prescription prescription = new Prescription();
        prescription.setBl_id(id);
        prescription.setCf_datetime(new Timestamp(new Date().getTime()));
        prescriptionMapper.insert(prescription);
        Prescription prescription1 = prescriptionMapper.getPrescriptionByBl_id(id);
        for (AdviceVO adviceVO : adviceVOS) {
            Advice advice = new Advice();
            Drug drug = drugMapper.getDrugByName(adviceVO.getYp_name());
            BeanUtils.copyProperties(case1,advice);
            BeanUtils.copyProperties(adviceVO,advice);
            BeanUtils.copyProperties(prescription1,advice);
            BeanUtils.copyProperties(drug,advice);
            double price = advice.getYz_amount() * drug.getYp_uprice();
            advice.setYz_price(price);
            allPrice = allPrice+price;
            adviceMapper.insert(advice);
        }
        Charge charge = new Charge();
        charge.setSf_datetime(new Timestamp(new Date().getTime()));
        charge.setSf_cost(allPrice);
        charge.setSf_charge(0);
        charge.setSf_refund(0);
        BeanUtils.copyProperties(case1,charge);
        chargeMapper.insert(charge);
    }

    @Override
    public List<CaseVO> selectRecord(){
        List<Case> cases = caseMapper.selectCaseRecord(1);
        return getCaseVOList(cases);
    }

    @Override
    public List<CaseVO> selectRecordByCard(long br_card){
        List<Case> cases = caseMapper.selectCaseRecordByCard(1,br_card);
        return getCaseVOList(cases);
    }

    @Override
    public List<CaseVO> selectTreat(){
        List<Case> cases = caseMapper.selectCaseRecord(0);
        return getCaseVOList(cases);
    }

    @Override
    public List<CaseVO> selectTreatByCard(long br_card){
        List<Case> cases = caseMapper.selectCaseRecordByCard(0,br_card);
        return getCaseVOList(cases);
    }

    @Override
    public List<AdviceVO> selectAdviceVO(long bl_id){
        List<AdviceVO> adviceVOS = new ArrayList<>();
        Prescription prescription = prescriptionMapper.getPrescriptionByBl_id(bl_id);
        List<Advice> adviceList = adviceMapper.getAdviceByPrescription(prescription.getCf_id());
        for (Advice advice : adviceList) {
            AdviceVO adviceVO = new AdviceVO();
            BeanUtils.copyProperties(advice,adviceVO);
            Drug drug = drugMapper.getDrugById(advice.getYp_id());
            BeanUtils.copyProperties(drug,adviceVO);
            adviceVOS.add(adviceVO);
        }
        return adviceVOS;
    }

    private List<CaseVO> getCaseVOList(List<Case> cases) {
        List<CaseVO> caseVOS = new ArrayList<>();
        for (Case aCase : cases) {
            CaseVO caseVO = new CaseVO();
            BeanUtils.copyProperties(aCase,caseVO);
            Patient patient = patientMapper.selectID(aCase.getBr_card());
            BeanUtils.copyProperties(patient,caseVO);
            if(patient.getBr_sex()==0){
                caseVO.setBr_sex("男");
            }else {
                caseVO.setBr_sex("女");
            }
            User user = userMapper.getUserById(patient.getYh_id());
            BeanUtils.copyProperties(user,caseVO);
            caseVOS.add(caseVO);
        }
        return caseVOS;
    }

    private List<CaseVO> getCaseVOS(List<Register> registers) {
        List<CaseVO> caseVOS = new ArrayList<>();
        for (Register register : registers) {
            CaseVO caseVO = new CaseVO();
            BeanUtils.copyProperties(register,caseVO);
            Patient patient = patientMapper.selectID(register.getBr_card());
            BeanUtils.copyProperties(patient,caseVO);
            if(patient.getBr_sex()==0){
                caseVO.setBr_sex("男");
            }else {
                caseVO.setBr_sex("女");
            }
            User user = userMapper.getUserById(patient.getYh_id());
            BeanUtils.copyProperties(user,caseVO);
            caseVOS.add(caseVO);
        }
        return caseVOS;
    }


    @Override
    public Case selectID(long id){
        return caseMapper.selectID(id);
    }
    @Override
    public void insert(Case case1){
        caseMapper.insert(case1);
    }
    @Override
    public void update(Case case1,long id){
        caseMapper.update(case1,id);
    }
}
